package cn.itcast.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	// 注入
	@Resource
	private CustomerRepository repository;

	@Override
	public List<Customer> findNoAssociationCustomers() {
		return repository.findByfixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
		return repository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreaId) {
		//解除关联动作
		repository.clearFixedAreaId(fixedAreaId);
		if (customerIdStr.equals("null")) {
			return;
		}
		String[] customerIdArray = customerIdStr.split(",");
		for (String id : customerIdArray) {
			Integer parseInt = Integer.parseInt(id);
			repository.updateFixedAreaId(fixedAreaId, parseInt);
		}
	}

	@Override
	public void regis(Customer customer) {
		repository.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		return repository.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		repository.updateType(telephone);
	}

	@Override
	public String findFixedAreaId(Integer fixedAreaId, String address) {
		
		return repository.findFixedAreaId(fixedAreaId,address);
	}
	
}
